﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

using EZCRS.HCMS.Entity;
using EZCRS.HCMS.Entity.Notify;
using EZCRS.HCMS.SqlClient.AIR;

using GFrame.Shadow;
using GFrame.Shadow.Search;

using OTA.Notification.Entity.Event;
using OTA.Notification.BO.Event;
using EZCRS.HCMS.Bll.baseinfo;
using Strive.DataAccess.Base.BO;
using EZCRS.HCMS.SqlClient;

namespace EZCRS.HCMS.Bll.AIR
{

    public class OTAMessageEventBO : MsgEventBO
    {

        protected override void ValidationRules()
        {
            //
        }
    }

    /// <summary>
    /// 变价通知
    /// </summary>
    public class AIR_Notify
    {

        private AirNotify _notifyData = new AirNotify();

        public enum NotifyProtocol
        {
            Email = 1, Fax,
            WebSerivce,
            MSN,
            QQ,
            Mobile
        }

        /// <summary>
        /// 填充模板
        /// </summary>
        /// <param name="notify"></param>
        /// <returns></returns>
        public string Render(RateNotify notify, string TemplateContent)
        {

            TemplateRender<RateNotify> tr = new TemplateRender<RateNotify>(notify);

            tr.Template = TemplateContent;

            return tr.Render();
        }

        /// <summary>
        /// 创建变价通知单
        /// </summary>
        /// <param name="logs"></param>
        /// <returns></returns>
        public int CreateNotify(List<AIR_LOG_CODE> Codes, string Name, string TemplateID, int PID, string Content, string Remark)
        {
            RateNotify ratenotify = CreateNotifyObject(Codes);

            HCRS_Hotel hotel = GetHotelInfo(PID);

            ratenotify.HotelName = hotel.subtitle;
            ratenotify.Title = Name;

            TemplateRender<RateNotify> tr = new TemplateRender<RateNotify>(ratenotify);

            tr.Template = Content;

            

            HCRS_AIR_NOTIFY airnotify = new HCRS_AIR_NOTIFY();

            airnotify.Name = Name;
            airnotify.TemplateID = TemplateID;
            airnotify.CreateDate = DateTime.Now;
            airnotify.PID = PID;
            airnotify.NotifyBody = tr.Render();

            

            return _notifyData.AddNotify(airnotify, Codes);

        }

        /// <summary>
        /// 根据DATATABLE创建
        /// </summary>
        /// <param name="StartDate"></param>
        /// <param name="EndDate"></param>
        /// <param name="AirTable"></param>
        /// <param name="Name"></param>
        /// <param name="TemplateID"></param>
        /// <param name="PID"></param>
        /// <param name="Content"></param>
        /// <returns></returns>
        public int CreateNotify(DateTime StartDate, DateTime EndDate, DataTable AirTable, string Name, string TemplateID, int PID, string Content, string Remark)
        {
            RateNotify notify = CreateNotifyObject(PID, StartDate, EndDate, AirTable);

            HCRS_Hotel hotel = GetHotelInfo(PID);

            notify.HotelName = hotel.subtitle;
            notify.Title = Name;
            notify.Remark = Remark;

            TemplateRender<RateNotify> tr = new TemplateRender<RateNotify>(notify);

            tr.Template = Content;

            HCRS_AIR_NOTIFY airnotify = new HCRS_AIR_NOTIFY();

            airnotify.Name = Name;
            airnotify.TemplateID = TemplateID;
            airnotify.CreateDate = DateTime.Now;
            airnotify.PID = PID;
            airnotify.NotifyBody = tr.Render();

            return _notifyData.AddNotify(airnotify);

        }

        public int DeleteNotify(int ID)
        {
            HCRS_AIR_NOTIFY del = new HCRS_AIR_NOTIFY();

            del.ID = ID;

            return _notifyData.DeleteNotify(del);
        }

        /// <summary>
        /// 获取酒店信息
        /// </summary>
        /// <param name="PID"></param>
        /// <returns></returns>
        private HCRS_Hotel GetHotelInfo(int PID)
        {
            sql_hcrs_hotel sqlHotel = new sql_hcrs_hotel();

            HCRS_Hotel hotel = sqlHotel.GetHotelByPID(PID);

            return hotel;
        }

        public HCRS_AIR_NOTIFY GetNotify(int ID)
        {
            return _notifyData.GetNotify(ID);
        }

        /// <summary>
        /// 发送到消息发送队列
        /// </summary>
        /// <param name="IATA"></param>
        /// <param name="Body"></param>
        /// <param name="Template"></param>
        /// <param name="SendType"></param>
        private void SendToMessageEvent(string IATA, HCRS_AIR_NOTIFY notifyobj, string SendType)
        {
            using (OTAMessageEventBO _eventBO = new OTAMessageEventBO())
            {
                MsgEventBean bean = new MsgEventBean();

                bean.AppCode = "HCRS";
                bean.CreateTime = DateTime.Now;
                bean.Level = "L";
                bean.MsgContent = notifyobj.NotifyBody;
                bean.MsgTemplate = notifyobj.TemplateID;
                bean.MsgSendType = SendType;
                bean.MsgTitle = notifyobj.Name;
                bean.Delay = 1;
                bean.DelayType = 1;
                bean.DelaySecond = 1;

                bean.ReceiverAccount = IATA;

                // _eventBO.IsNew = true;

                SaveAction sa = _eventBO.Save(bean);

            }
        }

        /// <summary>
        /// 发送到渠道的通知
        /// </summary>
        /// <param name="NotifyID"></param>
        /// <param name="protocol"></param>
        /// <param name="ChannelID"></param>
        /// <returns></returns>
        public int SendNotify(int NotifyID, NotifyProtocol protocol, int ChannelID)
        {

            bool b = _notifyData.InsertAddresseeBlockByChannel(NotifyID, ChannelID, protocol.ToString(), 1);

            if (b)
            {
                HCRS_AIR_NOTIFY airnotify = GetNotify(NotifyID);

                HCRS_TA_BLL tabll = new HCRS_TA_BLL();

                List<HCRS_TA> lst = tabll.GetItemsByChannel(ChannelID);

                List<MsgEventBean> lstevt = new List<MsgEventBean>();


                foreach (HCRS_TA ta in lst)
                {
                    MsgEventBean bean = new MsgEventBean();

                    bean.AppCode = "HCRS";
                    bean.CreateTime = DateTime.Now;
                    bean.Level = "L";
                    bean.MsgContent = airnotify.NotifyBody;
                    bean.MsgTemplate = airnotify.TemplateID;
                    bean.MsgSendType = protocol.ToString();
                    bean.MsgTitle = airnotify.Name;
                    bean.Delay = 1;
                    bean.DelayType = 1;
                    bean.DelaySecond = 1;

                    bean.ReceiverAccount = ta.IATA;

                    lstevt.Add(bean);
                    //SendToMessageEvent(ta.IATA, airnotify.NotifyBody, airnotify.TemplateID, protocol.ToString());
                }

                int iRow = 0;

                using (OTAMessageEventBO _eventBO = new OTAMessageEventBO())
                {
                    iRow = _eventBO.SaveList(lstevt);
                }


                return iRow;
            }

            return -4;
        }

        /// <summary>
        /// 发送到TA的通知
        /// </summary>
        /// <param name="NotifyID"></param>
        /// <param name="protocol"></param>
        /// <param name="IATA"></param>
        /// <returns></returns>
        public int SendNotify(int NotifyID, NotifyProtocol protocol, string IATA)
        {
            HCRS_AIR_NOTIFY_ADDRESS address = new HCRS_AIR_NOTIFY_ADDRESS();

            address.NotifyID = NotifyID;

            address.IATA = IATA;

            address.Protocol = protocol.ToString();
            address.CreateDate = DateTime.Now;

            address.State = 1;

            Shadow<HCRS_AIR_NOTIFY_ADDRESS> sha = new Shadow<HCRS_AIR_NOTIFY_ADDRESS>(address);

            sha.ConnectionName = MainBase.CONNECTION_STRING;

            bool b = sha.Save();

            if (b)
            {
                //发送至msg_event表
                HCRS_AIR_NOTIFY airnotify = GetNotify(NotifyID);

                SendToMessageEvent(IATA, airnotify, protocol.ToString());

                return 0;
            }

            return -4;
        }


        public List<HCRS_AIR_NOTIFY> GetItemsByPID(int PID, string Query, int PageIndex, int PageSize, out int RecordCount, out int PageCount)
        {

            return _notifyData.GetItemsByPID(PID, Query, PageIndex, PageSize, out RecordCount, out PageCount);

        }

        public List<AIR_Log> GetSameDateLog(AIR_Log log)
        {
            AirLog airlog = new AirLog();

            return airlog.SearchLog(log.PID, log.StartDate, log.EndDate);

        }

        /// <summary>
        /// 根据AIR_LOG_CODE建立通知实体类
        /// </summary>
        /// <param name="codes"></param>
        /// <returns></returns>
        public RateNotify CreateNotifyObject(List<AIR_LOG_CODE> codes)
        {
            RateNotify notify = new RateNotify();

            EZCRS.HCMS.Bll.AIR.AIR air = new EZCRS.HCMS.Bll.AIR.AIR();

            string strHtml = "";

            strHtml += "<table>";
            strHtml += "<tr class=\"tb_title\">";
            strHtml += "<td>房型代码</td>";
            strHtml += "<td>价格代码</td>";
            strHtml += "<td>单人价</td>";
            strHtml += "<td>双人价</td>";
            strHtml += "<td>三人价</td>";
            strHtml += "<td>四人价</td>";
            strHtml += "<td>含早</td>";
            strHtml += "</tr>";

            foreach (AIR_LOG_CODE code in codes)
            {
                AIR_Log log = air.GetLogByID(code.LogID);

                if (log != null)
                {
                    notify.StartDate = log.StartDate;
                    notify.EndDate = log.EndDate;

                    strHtml += "<tr>";
                    strHtml += "<td>" + code.RoomCode + "</td>";
                    strHtml += "<td>" + code.PlanCode + "</td>";
                    strHtml += "<td>" + log.Rate1.ToString("C") + "</td>";
                    strHtml += "<td>" + log.Rate2.ToString("C") + "</td>";
                    strHtml += "<td>" + log.Rate3.ToString("C") + "</td>";
                    strHtml += "<td>" + log.Rate4.ToString("C") + "</td>";

                    if (log.HasBreakfast)
                        strHtml += "<td>含早</td>";
                    else
                        strHtml += "<td>不含</td>";

                    strHtml += "</tr>";
                }
            }

            strHtml += "</table>";
            
            notify.Table = strHtml;

            return notify;
        }

        /// <summary>
        /// 建立通知单实体类
        /// </summary>
        /// <param name="PID"></param>
        /// <param name="StartDate"></param>
        /// <param name="EndDate"></param>
        /// <param name="AirTable"></param>
        /// <returns></returns>
        public RateNotify CreateNotifyObject(int PID, DateTime StartDate, DateTime EndDate, DataTable AirTable)
        {
            sql_hcrs_roomcode sqlRoom = new sql_hcrs_roomcode();

            DataView dv = AirTable.DefaultView;

            dv.Sort = "RoomCode";

            RateNotify notify = new RateNotify();

            string strHtml = "";

            strHtml += "<table>";
            strHtml += "<tr class=\"tb_title\">";
            strHtml += "<td>房型代码</td>";
            strHtml += "<td>价格代码</td>";
            strHtml += "<td>单人价</td>";
            strHtml += "<td>双人价</td>";
            strHtml += "<td>三人价</td>";
            strHtml += "<td>四人价</td>";
            strHtml += "<td>含早</td>";
            strHtml += "</tr>";

            foreach (DataRow dr in dv.ToTable().Rows)
            {
                notify.StartDate = StartDate;
                notify.EndDate = EndDate;

                float f1 = 0, f2 = 0, f3 = 0, f4 = 0;

                float.TryParse(dr["Rate1"].ToString(), out f1);
                float.TryParse(dr["Rate2"].ToString(), out f2);
                float.TryParse(dr["Rate3"].ToString(), out f3);
                float.TryParse(dr["Rate4"].ToString(), out f4);

                HCRS_RoomCode room = sqlRoom.getItem(PID, dr["RoomCode"].ToString());

                strHtml += "<tr>";

                if(room != null)
                    strHtml += "<td>" + room.roomname + "(" + dr["RoomCode"].ToString() + ")</td>";
                else
                    strHtml += "<td>" + dr["RoomCode"].ToString() + "</td>";

                strHtml += "<td>" + dr["RateCode"].ToString() + "</td>";
                strHtml += "<td>" + f1.ToString("C") + "</td>";
                strHtml += "<td>" + f2.ToString("C") + "</td>";
                strHtml += "<td>" + f3.ToString("C") + "</td>";
                strHtml += "<td>" + f4.ToString("C") + "</td>";

                if (dr["BF"].ToString() == "true")
                    strHtml += "<td>含早</td>";
                else
                    strHtml += "<td>不含</td>";

                strHtml += "</tr>";

            }

            strHtml += "</table>";

            notify.Table = strHtml;

            return notify;
        }

    }
}
